using System;
using System.Net;

namespace WindowsApplication4
{
	/// <summary>
	/// Summary description for CHttpEngine.
	/// </summary>
	public class CHttpEngine
	{
		public CHttpEngine()
		{
		}

		public System.IO.StreamReader POSTData (string sURL, string sData) {
			if (!sURL.StartsWith("http://")) {
				sURL = "http://" + sURL.Trim();
			} else {
				sURL = sURL.Trim();
			}	

			System.Net.HttpWebResponse myResponse= null;
			System.Net.HttpWebRequest myRequest=null ;
			
		
			// Initialize the WebRequest.
			myRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(sURL);
			myRequest.Method = "POST";
			myRequest.ContentType = "application/X-www-form-urlencoded";
			myRequest.ContentLength = sData.Length;
			System.IO.StreamWriter sw = new System.IO.StreamWriter(myRequest.GetRequestStream());
			sw.Write(sData);
			sw.Close();

			try {
				myResponse = (System.Net.HttpWebResponse)myRequest.GetResponse ();
			} catch (WebException ex) {
				throw ex;
			}
			
			System.IO.StreamReader sr= null;
			if (myResponse != null) {
				 sr = new System.IO.StreamReader(myResponse.GetResponseStream());
			}
			return sr;
		}



		public System.IO.StreamReader GETData (string sURL, string sURLData) {
			if (!sURL.StartsWith("http://")) {
				sURL = "http://" + sURL.Trim();
			} else {
				sURL = sURL.Trim();
			}	

			System.Net.HttpWebResponse myResponse= null;
			System.Net.HttpWebRequest myRequest=null ;

			if (sURLData != null && sURLData != "") 
			{
				sURL = sURL + "?" + sURLData;
			}

			// Return the response. 
			try {
				// Initialize the WebRequest.
				myRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(sURL);
                myRequest.Timeout = 1000000;


                if (WindowsApplication4.Properties.Settings.Default.useProxy)
                {
                    WebProxy myProxy = new WebProxy();
                    myProxy.Address = new Uri(WindowsApplication4.Properties.Settings.Default.proxyAddress);
                    myProxy.UseDefaultCredentials = true;
                    myRequest.Proxy = myProxy;
                }

				myResponse = (System.Net.HttpWebResponse)myRequest.GetResponse ();
			} catch (Exception ex) {
				throw ex;
			}
			
			System.IO.StreamReader sr= null;
			if (myResponse != null) {
                //Marko: change for gzip stream
                System.IO.Compression.GZipStream gz = 
                    new System.IO.Compression.GZipStream(myResponse.GetResponseStream(), 
                                                         System.IO.Compression.CompressionMode.Decompress);
				sr = new System.IO.StreamReader(gz);
                //sr = new System.IO.StreamReader(myResponse.GetResponseStream());
			}
			return sr;
		}
	}
}
